package ru.st.selenium.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;

/**
 * Created with IntelliJ IDEA.
 * User: gor
 * Date: 02.11.13
 * Time: 19:38
 * To change this template use File | Settings | File Templates.
 */
public class AddFilmPage extends AnyPage {

    public AddFilmPage(PageManager pages) {
        super(pages);
    }


    @FindBy(name = "imdbid")
    private WebElement imdbidField;

    @FindBy(name = "name")
    private WebElement nameField;

    @FindBy(name = "year")
    private WebElement yearField;

    @FindBy(name = "submit")
    private WebElement submitButton;

    public AddFilmPage setImdbField(String text) {
        imdbidField.sendKeys(text);
        return this;
    }

    public AddFilmPage setNameField(String text) {
        nameField.sendKeys(text);
        return this;
    }

    public AddFilmPage setYearField(String text) {
        yearField.sendKeys(text);
        return this;
    }

    public void clickSubmitButton() {
        submitButton.click();
    }

    public AddFilmPage ensurePageLoaded() {
        super.ensurePageLoaded();
        wait.until(presenceOfElementLocated(By.name("submit")));
        return this;
    }

    public AddFilmPage ensureFilmAdded() {
        super.ensurePageLoaded();
        wait.until(presenceOfElementLocated(By.xpath("//img[@title='Edit']")));
        return this;
    }

    public AddFilmPage ensureFilmNotAdded(){
        super.ensurePageLoaded();
        wait.until(presenceOfElementLocated(By.name("submit")));
        System.out.println("Name error = " + driver.findElement(By.xpath("//input[@name='name']/../label")).getText());
        System.out.println("Year error = " + driver.findElement(By.xpath("//input[@name='year']/../label")).getText());

        return this;
    }
}
